package com.hqyj.system.controller;

import com.hqyj.system.model.SysUser;
import com.hqyj.system.service.SysUserService;
import com.hqyj.system.service.impl.SysUserServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class LoginController {
    //注入业务接口
    SysUserService sysUserService = new SysUserServiceImpl();

    @RequestMapping("login")
    public String Login(HttpServletRequest request, HttpServletResponse response, Model model, SysUser sysUser) throws IOException {
        //1.接受数据
        System.out.println("LoginController----------Login()");
        //3.调用业务
        SysUser sysUser_db = sysUserService.querySysUserByNameAndPassword(sysUser);
        if (sysUser_db != null) {
            if (sysUser.getUsername().equals(sysUser_db.getUsername()) && sysUser.getPassword().equals(sysUser.getPassword())) {
                //登陆用户信息保存session
                HttpSession ss = request.getSession();
                ss.setAttribute("SYSUSER", sysUser_db);
                //1.友情提示
                model.addAttribute("message", "登录成功!");
                ///2.跳转页面
                return "view/frame/index";
            } else {
                model.addAttribute("message", "登录失败，用户名或密码错误!");
                return "login";
            }
        } else {
            model.addAttribute("message", "登录失败，用户名或密码错误!");
            return "login";
        }
    }
}
